const charset = require('superagent-charset');
let superagent =  charset(require('superagent'));
require('superagent-proxy')(superagent);
const sdate = require('silly-datetime');
const fetch = require('node-fetch');
const ProxyAgent = require('proxy-agent');
const HttpsProxyAgent = require("https-proxy-agent");

require('superagent-proxy')(superagent);


//const got = require('got');

const {
    getTaskPage,
    wxpusher,
} = require('./ti_scan_config');

const {
    readTask,
} = require('./ti_logic_read_task');

const {
    logger,
    sleep,
    httpGet,
    httpPost,
    getProxy,
    getIp,
    getIp_v5,
    getIp_v6,
    httpProxyGet,
    httpProxyGet2,
    httpProxyGet3,
    httpProxyGet4,
    tiGet,
} = require('./util');

const {
    get,
    easyPushWechatMsg,
    lpush,
    set,
    hget,
    hset,
    del,
    expire,
    hgetall,
    randBetween,
    avoidNoticeFreq,
    avoidNoticeFreqCookie,
    avoidNoticeFreqByKey,
} = require('../util');

const {
    syncExec,
    syncExec2
} = require('../model'); 

process.on('unhandledRejection', error => {
    //...不处理...
});

const serialMap = {};//已启动到任务

let ip_list = [];

let arguments = process.argv.splice(2);

let process_num = 70;

const updateProxySlow = async () => {

    let key = 'new_proxy_list_'+arguments[0];

    //ip_list = await get(key);

    if(ip_list){
        //ip_list = JSON.parse(ip_list);
    }else{
        ip_list = [];
    }

    let now_time = new Date().getTime();

    

    if(ip_list.length>0){
        for(let m=0; m<ip_list.length; m++){
            if(ip_list[m].expire_time - now_time/1000 <25){
                ip_list.splice(m, 1);
            }
        }
    }

    if(ip_list.length < process_num+25){

        //logger(4444444);
        let new_ip_list = await getIp_v5(process_num-ip_list.length+25);

        //logger('new_ip_list', new_ip_list);

        if(!new_ip_list){
            await sleep(2000);
            new_ip_list = await getIp_v5(process_num-ip_list.length+25);
        }

        for(let m=0; m<new_ip_list.length; m++){
            ip_list.push(new_ip_list[m]);
        }
    }

    //await set(key, JSON.stringify(ip_list));
}

//每30秒重新拉取任务列表
(async () => {
    while (1) {
        //await updateProxySlow();
        await sleep(10000);
    }
})();



const delete_proxy = async (proxy, serial) => {

    for(let m=0; m<ip_list.length; m++){
        if(ip_list[m].ip == proxy || ip_list[m].serial == serial){
            //ip_list.splice(m, 1);
        }
    }
}

const update_fail_proxy = async (proxy, serial) => {

    let is_del = 0;

    for(let m=0; m<ip_list.length; m++){
        if(ip_list[m].ip == proxy || ip_list[m].serial == serial){
            
            if(ip_list[m].fail_num == undefined || ip_list[m].fail_num == 'NAN'){
                ip_list[m].fail_num = 1;
            }else{
                ip_list[m].fail_num = ip_list[m].fail_num + 1;
            }

            if(ip_list[m].fail_num >= 3){
                // let key = 'set_cookie_'+serial+'_'+proxy;
                // logger('删除缓存' + key);
                // await del(key);
            }

            if(ip_list[m].fail_num >= 10){
                //  失败超过2次就换ip
                //logger('删除proxy', proxy+' '+serial+' '+ip_list.length)
                ip_list.splice(m, 1);

                let today = sdate.format(new Date(), 'YYYY-MM-DD');

                const del_ip_key = 'del_ip_log_'+ today;

                await lpush(del_ip_key, JSON.stringify({'serial':serial, 'proxy':proxy, 'time':sdate.format(new Date(), 'YYYY-MM-DD HH:mm:ss'), 'expire_time': ip_list[m].expire_time, 'machine':'机器'+arguments[0]}));

                is_del = 1;
                logger('删除proxy', proxy+' '+serial+' '+ip_list.length)
            }
        }
    }
    if(is_del == 1){
        for(var j in ip_list){
            if(ip_list[j].serial == '' || ip_list[j].serial == undefined){
                ip_list[j].serial = serial;
                return ip_list[j];
            }
        }
    }

    //logger('删除完成');

}

const update_success_proxy = async (proxy, serial) => {

    for(let m=0; m<ip_list.length; m++){
        if(ip_list[m].ip == proxy || ip_list[m].serial == serial){

            if(ip_list[m].fail_num > 0){
                logger(serial+ '从' + ip_list[m].fail_num + '重置为0');
            }
            
            ip_list[m].fail_num = 0;

            
        }
    }

}


const getProxySlow = async (serial) => {
    let proxy = '';

    let key = 'new_proxy_list_'+arguments[0];

    //logger('ip_list', ip_list);

    logger('find_proxy_', serial);

    //ip_list = await get(key);

    if(ip_list){
        //ip_list = JSON.parse(ip_list);
    }else{
        ip_list = [];
    }

    if(ip_list.length == 0){
        return '';
    }

    for(var j in ip_list){
        if(ip_list[j].serial == serial){
            return ip_list[j];
        }
    }

    

    for(var j in ip_list){
        if(ip_list[j].serial == '' || ip_list[j].serial == undefined){
            ip_list[j].serial = serial;

            //await set(key, JSON.stringify(ip_list));

            return ip_list[j];
        }
    } 

    //console.log('ip_length', ip_list.length);

    return proxy;
}

const httpPost2 = (url, proxy) => {
    return new Promise((resolve, reject) => {
        superagent.agent().get(url).timeout(2000)
            .end((err, res) => {
                if (err) {
                    return reject(err);
                }
                resolve(res.text);
            });
    });
}

//每N秒重新拉取任务列表
(async () => {
    const url = 'https://www.ti.com.cn/storeservices/cart/opninventory?opn=';



    const tasks = await readTask(1,350);
    //logger(tasks);

    let fullUrl = url+ tasks[0].serial;

    logger(fullUrl);

    //fullUrl = 'https://www.ti.com.cn/storeservices/cart/opninventory?opn=TCA9617BDGKR';
    while(1){
        let str = await httpPost2('https://www.ti.com.cn/productmodel/LM25575/tistore?locale=zh-CN', '', '');

        logger(str);

        await sleep(1000);
    }

    

    let task_list = '';

    for(i in tasks){
        if(tasks[i].status == 1){
            task_list += tasks[i].serial+',';
        }
    }
    //logger(task_list);

})();
